namespace ContribSample.jQueryUI
{
	using System;
	using System.Collections;
	using System.Html;
	using jQueryApi;
	using Script.Contrib.jQueryUI;

	public class DialogSamples
	{
		/// <summary>
		/// Default Functionality
		/// </summary>
		public static void Dialog()
		{
			jQueryUI.Select("#dialog").Dialog();
		}

		/// <summary>
		/// Animated Dialog
		/// </summary>
		public static void Animated()
		{
			Dictionary dialogOptions = new Dictionary("autoOpen", false, "show", "blind", "hide", "explode");
			jQueryUI.Select("#dialog").Dialog(dialogOptions);

			jQuery.Select("#opener").Click(delegate(jQueryEvent e)
			{
				jQueryUI.Select("#dialog").Dialog("open");
			});
		}

		/// <summary>
		/// Modal Dialog
		/// </summary>
		public static void Modal()
		{
			Dictionary dialogOptions = new Dictionary("height", 140, "modal", true);
			jQueryUI.Select("#dialog").Dialog(dialogOptions);

		}
		
		/// <summary>
		/// Modal Message
		/// </summary>
		public static void ModalMessage()
		{
			jQueryEventHandler clickEvent = delegate(jQueryEvent e) { jQueryUI.Select(jQuery.This).Dialog("close"); };
			Dictionary dialogOptions = new Dictionary("height", 140, "buttons", 
				new Dictionary("Ok", clickEvent));
			jQueryUI.Select("#dialog").Dialog(dialogOptions);
		}
		
		/// <summary>
		/// Modal Confirmation
		/// </summary>
		public static void ModalConfirmation()
		{
			jQueryEventHandler closeEvent = delegate(jQueryEvent e) { jQueryUI.Select(jQuery.This).Dialog("close"); };
			Dictionary dialogOptions = new Dictionary("resizable", false, 
				"height", 140, 
				"modal", true,
				"buttons", new Dictionary("Delete all items", closeEvent, "Cancel", closeEvent));
			jQueryUI.Select("#dialog").Dialog(dialogOptions);

		}
		/// <summary>
		/// Modal Form
		/// </summary>
		public static void ModalForm()
		{
			jQueryObject name = jQuery.Select("#name");
			jQueryObject email = jQuery.Select("#email");
			jQueryObject password = jQuery.Select("#password");
			jQueryObject allFields = jQueryUI.FromObject(new Array()).Add(name).Add(email).Add(password);
			jQueryObject tips = jQuery.Select(".validateTips");

			UpdateTips updateTips = delegate(string tip)
			{
				tips.Text(tip)
					.AddClass("ui-state-highlight");
				Window.SetTimeout(delegate() {
					tips.RemoveClass("ui-state-highlight");
				}, 500);
			};

			CheckLength checkLength = delegate(jQueryObject o, string oName, int min, int max)
			{
				if (o.GetValue().Length > max || o.GetValue().Length < min)
				{
					o.AddClass("ui-state-error");
					updateTips("Length of " + oName + " must be between " + min + " and " + max + ".");
					return false;
				}
				return true;
			};

			CheckRegexp checkRegExp = delegate(jQueryObject o, RegularExpression regexp, string oName)
			{
				if (!regexp.Test(o.GetValue()))
				{
					o.AddClass("ui-state-error");
					updateTips(oName);
					return false;
				}
				return true;
			};

			jQueryEventHandler createAccountEvent = delegate(jQueryEvent e) {
				bool allValid = true;
				allFields.RemoveClass("ui-state-error");

				allValid = allValid && checkLength(name, "username", 3, 16);
				allValid = allValid && checkLength(email, "email", 6, 80);
				allValid = allValid && checkLength(password, "password", 5, 16);

				allValid = allValid && checkRegExp(name, new RegularExpression("^[a-z]([0-9a-z_])+$", "i"), "Username may consist of a-z, 0-9, underscores, begin with a letter.");
				allValid = allValid && checkRegExp(email, new RegularExpression("^((([a-z]|\\d|[!#\\$%&'\\*\\+\\-\\/=\\?\\^_`{\\|}~]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])+(\\.([a-z]|\\d|[!#\\$%&'\\*\\+\\-\\/=\\?\\^_`{\\|}~]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])+)*)|((\\x22)((((\\x20|\\x09)*(\\x0d\\x0a))?(\\x20|\\x09)+)?(([\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x7f]|\\x21|[\\x23-\\x5b]|[\\x5d-\\x7e]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])|(\\\\([\\x01-\\x09\\x0b\\x0c\\x0d-\\x7f]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF]))))*(((\\x20|\\x09)*(\\x0d\\x0a))?(\\x20|\\x09)+)?(\\x22)))@((([a-z]|\\d|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])|(([a-z]|\\d|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])([a-z]|\\d|-|\\.|_|~|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])*([a-z]|\\d|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])))\\.)+(([a-z]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])|(([a-z]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])([a-z]|\\d|-|\\.|_|~|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])*([a-z]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])))\\.?$", "i"), "eg. ui@jquery.com");
				allValid = allValid && checkRegExp(password, new RegularExpression("^([0-9a-zA-Z])+$"), "Password field only allow : a-z 0-9");

				if (allValid)
				{
					jQuery.Select("#users tbody")
						.Append("<tr><td>" + name.GetValue() + "</td><td>" + email.GetValue() + "</td><td>" + password.GetValue() + "</td></tr>");
					jQueryUI.Select(jQuery.This).Dialog("close");
				}
			};
			
			jQueryEventHandler closeEvent = delegate(jQueryEvent e) {
				jQueryUI.Select(jQuery.This).Dialog("close"); 
			};
			
			jQueryEventHandler closeHandler = delegate(jQueryEvent e) {
				allFields.Value(string.Empty).RemoveClass("ui-state-error");
			};

			Dictionary dialogOptions = new Dictionary(
				"autoOpen", false, 
				"height", 300, 
				"width", 350,
				"modal", true,
				"buttons", new Dictionary(
					"Create an account", createAccountEvent, 
					"Close", closeEvent
					),
				"close", closeHandler);

			jQueryUI.Select("#dialog").Dialog(dialogOptions);

			jQuery.Select("#create-user").Click(delegate(jQueryEvent e) { jQueryUI.Select("#dialog").Dialog("open"); });
		}
	}

	delegate void UpdateTips(string tip);
	delegate bool CheckLength(jQueryObject o, string name, int min, int max);
	delegate bool CheckRegexp(jQueryObject o,RegularExpression regexp, string oName);
}
